package practice05;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Scanner;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: practice05
 * @ClassName: practice07.Server
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2020/1/3 9:38
 * @Copyright 大牛版板所有
 */
public class Server {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        byte[] bytes = new byte[1024];
        DatagramPacket dpt = null;
        DatagramSocket dst = null;
        String talks = null;
        String response = null;
        try {
            dpt = new DatagramPacket(bytes, 1024);
            dst = new DatagramSocket(8000);
            System.out.println("服务器已经启动");
            while (true) {
                dst.receive(dpt);
                talks = new String(dpt.getData(),0,dpt.getLength());
                System.out.println("客户端说："+talks);
                System.out.print("请输入回复内容：");
                response = input.next();
                dpt.setData(response.getBytes(),0,response.getBytes().length);
                dst.send(dpt);
            }
        }catch (Exception e) {
                e.printStackTrace();
        }finally {
            if (dst != null){
                dst.close();
            }
        }
    }
}
